package com.ubs.training.web.controller;

import com.ubs.training.server.dao.model.Contract;
import com.ubs.training.server.service.ContractService;
import com.ubs.training.server.service.ProductService;
import com.ubs.training.web.util.ContractValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.DataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/22/12
 * Time: 3:47 AM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class ContractsController {

    @Autowired
    ContractService contractService;

    @Autowired
    ProductService productService;

    @InitBinder
    public void initBind(DataBinder bind){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setLenient(false);
        bind.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
        bind.registerCustomEditor(Double.TYPE, new CustomNumberEditor(Double.class, new DecimalFormat("0.00"), true));
    }



    @RequestMapping(value = "client/{clientId}/contract/list.do", method = RequestMethod.GET)
    public String listContracts(@PathVariable("clientId") int clientId, Model model) {
        model.addAttribute("contracts", contractService.getByClientId(clientId));
        model.addAttribute("clientId", clientId);
        return "listcontracts";
    }

    @RequestMapping(value = "client/{clientId}/contract/{contractId}/edit.do", method = RequestMethod.GET)
    public String getContract(@PathVariable("clientId") int clientId,
                              @PathVariable("contractId") int contractId, Model model) {
        Contract contract;
        if(contractId > 0) {
            contract = contractService.getById(contractId);
        } else {
            contract = new Contract();
            contract.setIdClient(clientId);
        }

        model.addAttribute("contract", contract);
        model.addAttribute("products", productService.getByClientId(clientId));
        return "editcontract";
    }

    @RequestMapping(value = "client/{clientId}/contract/{contractId}/edit.do", method = RequestMethod.POST)
    public String saveContract(@PathVariable("clientId") int clientId,
                               @PathVariable("contractId") int contractId,
                               @ModelAttribute("contract") Contract contract, BindingResult res,
                               Model model) {
        new ContractValidator(contract, res).validate();
        if (res.hasErrors()) {

            model.addAttribute("contract", contract);
            model.addAttribute("products", productService.getByClientId(clientId));
            return "editcontract";
        }
        contract.setIdContract(contractId);
        contract.setIdClient(clientId);
        contractService.save(contract);
        return "redirect:/client/{clientId}/contract/list.do";
    }

    @RequestMapping(value = "client/{clientId}/contract/{contractId}/delete.do", method = RequestMethod.GET)
    public String deleteContract(@PathVariable("clientId") int clientId,
                                 @PathVariable("contractId") int contractId, Model model) {
        contractService.delete(contractId);
        return "redirect:/client/{clientId}/contract/list.do";
    }

}
